#include "InitializeParameter.h"


InitializeParameter::InitializeParameter(void)
{
}


InitializeParameter::~InitializeParameter(void)
{
	Save();
}

void InitializeParameter::Read()
{
	std::string paraFileName("Parameter.ini");
	std::fstream paraFile(paraFileName,std::fstream::in);
	//std::istringstream	extractString;

	std::string strOneLine;
	//std::string	paraName;
	//std::string name;

	while(!paraFile.eof())
	{
		getline(paraFile,strOneLine);

		if(strOneLine == "[Camera]")
		{
			ReadCamera(paraFile);
		}//end if
		else if(strOneLine == "[Light]")
		{
			ReadLight(paraFile);
		}//end else
		else if(strOneLine == "[AI]")
		{
			ReadAIParameter(paraFile);
		}
		else if(strOneLine == "[Other]")
		{
			ReadOtherParameter(paraFile);
		}
		else if(strOneLine == "[CEGUI]")
		{
			ReadCEGUIParameter(paraFile);
		}

	}//end while
}

D3DXVECTOR3 InitializeParameter::GetCameraPos()
{
	return m_vec3CameraPosition;
}

D3DXVECTOR3 InitializeParameter::GetCameraLookAt()
{
	return m_vec3CameraLookAt;
}

D3DXVECTOR3 InitializeParameter::GetCameraUp()
{
	return m_vec3CameraUp;
}

double InitializeParameter::GetCameraSpeed()
{
	return m_dfCameraSpeed;
}

double InitializeParameter::GetCameraPitchFactor()
{
	return m_dfCameraPitchFactor;
}

double InitializeParameter::GetCameraYawFactor()
{
	return m_dfCameraYawFactor;
}

void InitializeParameter::SetCameraVector3( const std::string& name,float x,float y,float z )
{
	if(name == "Position")
	{
		m_vec3CameraPosition.x = x;
		m_vec3CameraPosition.y = y;
		m_vec3CameraPosition.z = z;
	}
	else if(name == "LookAt")
	{
		m_vec3CameraLookAt.x = x;
		m_vec3CameraLookAt.y = y;
		m_vec3CameraLookAt.z = z;
	}
	else if(name == "Up")
	{
		m_vec3CameraUp.x = x;
		m_vec3CameraUp.y = y;
		m_vec3CameraUp.z = z;
	}
}

void InitializeParameter::SetCameraFactor( const std::string& name , float factor )
{
	if(name == "Speed")
		m_dfCameraSpeed = factor;
	else if(name == "pitchFactor")
		m_dfCameraPitchFactor = factor;
	else if(name == "yawFactor")
		m_dfCameraYawFactor = factor;
	else if(name == "rollFactor")
		m_dfCameraRollFactor = factor;
}

inline void InitializeParameter::ReadCamera( std::fstream& paraFile )
{
	std::istringstream	extractString;
	std::string strOneLine;
	std::string	paraName;
	std::string name;
	char no_use;
	float x,y,z;

	//read the camera vector3
	for(int i=0 ;i<3; ++i)
	{
		getline(paraFile,strOneLine);
		extractString.clear();
		extractString.str(strOneLine);

		extractString>>name>>no_use>>x>>y>>z;
		SetCameraVector3(name,x,y,z);
	}//end for

	//read the camera factor
	for(int i=0 ;i<4 ;++i)
	{
		getline(paraFile,strOneLine);
		extractString.clear();
		extractString.str(strOneLine);

		extractString>>name>>no_use>>x;
		SetCameraFactor(name,x);
	}//end for
}

double InitializeParameter::GetCameraRollFactor()
{
	return m_dfCameraRollFactor;
}

void InitializeParameter::SaveCameraPara( const D3DXVECTOR3& pos,const D3DXVECTOR3& lookAt ,const D3DXVECTOR3& up )
{
	m_vec3CameraPosition = pos;
	m_vec3CameraLookAt = lookAt;
	m_vec3CameraUp = up;
}

void InitializeParameter::WriteCamera( std::fstream& paraFile )
{
	paraFile<<"[Camera]"<<std::endl;

	paraFile<<"Position = ";
	WriteD3DXVECTOR3(paraFile,m_vec3CameraPosition);

	paraFile<<"LookAt = ";
	WriteD3DXVECTOR3(paraFile,m_vec3CameraLookAt);

	paraFile<<"Up = ";
	WriteD3DXVECTOR3(paraFile,m_vec3CameraUp);

	paraFile<<"Speed = "<<m_dfCameraSpeed<<std::endl;
	paraFile<<"pitchFactor = "<<m_dfCameraPitchFactor<<std::endl;
	paraFile<<"yawFactor = "<<m_dfCameraYawFactor<<std::endl;
	paraFile<<"rollFactor = "<<m_dfCameraRollFactor<<std::endl;
}

void InitializeParameter::WriteD3DXVECTOR3( std::fstream& file,const D3DXVECTOR3& vec )
{
	file<<vec.x<<" "<<vec.y<<" "<<vec.z<<std::endl;
}

void InitializeParameter::Save()
{
	std::string paraFileName("Parameter.ini");
	std::fstream paraFile(paraFileName,
				std::fstream::in | std::fstream::out | std::ios_base::trunc);
	WriteCamera(paraFile);
	WriteLight(paraFile);
	WriteAIParameter(paraFile);
	WriteCEGUIParameter(paraFile);
	WriteOtherParameter(paraFile);
}

void InitializeParameter::ReadLight( std::fstream& paraFile )
{
	std::istringstream	extractString;
	std::string strOneLine;
	std::string	paraName;
	std::string name;
	char no_use;
	float x,y,z,a;

	getline(paraFile,strOneLine);
	extractString.clear();
	extractString.str(strOneLine);

	extractString>>name>>no_use>>x>>y>>z;
	SetLightDirection(x,y,z);

	//read the camera vector3
	for(int i=0 ;i<3; ++i)
	{
		getline(paraFile,strOneLine);
		extractString.clear();
		extractString.str(strOneLine);

		extractString>>name>>no_use>>x>>y>>z>>a;
		SetLightAttribute(name,x,y,z,a);
	}//end for
}

void InitializeParameter::SetLightDirection( float x,float y,float z )
{
	m_Light.dir.x = x;
	m_Light.dir.y = y;
	m_Light.dir.z = z;
}

void InitializeParameter::SetLightAttribute( const std::string& name,float x,float y,float z,float a )
{
	if(name == "Ambient")
	{
		m_Light.ambient.r = x;
		m_Light.ambient.g = y;
		m_Light.ambient.b = z;
		m_Light.ambient.a = a;
	}
	else if(name == "Diffuse")
	{
		m_Light.diffuse.r = x;
		m_Light.diffuse.g = y;
		m_Light.diffuse.b = z;
		m_Light.diffuse.a = a;
	}
	else if(name == "Specular")
	{
		m_Light.specular.r = x;
		m_Light.specular.g = y;
		m_Light.specular.b = z;
		m_Light.specular.a = a;
	}
}

void InitializeParameter::WriteLight( std::fstream& paraFile )
{
	paraFile<<"[Light]"<<std::endl;

	paraFile<<"Direction = ";
	WriteD3DXVECTOR3(paraFile,m_Light.dir);

	paraFile<<"Ambient = ";
	WriteD3DXCOLOR(paraFile,m_Light.ambient);

	paraFile<<"Diffuse = ";
	WriteD3DXCOLOR(paraFile,m_Light.diffuse);

	paraFile<<"Specular = ";
	WriteD3DXCOLOR(paraFile,m_Light.specular);
}

void InitializeParameter::WriteD3DXCOLOR( std::fstream& file , const D3DXCOLOR& color )
{
	file<<color.r<<" "<<color.g<<" "<<color.b<<" "<<color.a<<std::endl;
}

Light InitializeParameter::GetLight()
{
	return m_Light;
}

void InitializeParameter::ReadAIParameter( std::fstream& paraFile )
{
	std::istringstream	extractString;
	std::string strOneLine;
	std::string name;
	char no_use;
	int depth;

	getline(paraFile,strOneLine);
	extractString.clear();
	extractString.str(strOneLine);

	extractString>>name>>no_use>>depth;
	m_iAILevel = depth;
}

int InitializeParameter::GetAILevel()
{
	return m_iAILevel;
}

void InitializeParameter::WriteAIParameter( std::fstream& paraFile )
{
	paraFile<<"[AI]"<<std::endl;

	paraFile<<"Level = "<<m_iAILevel<<std::endl;
}

void InitializeParameter::ReadOtherParameter( std::fstream& paraFile )
{
	std::istringstream	extractString;
	std::string strOneLine;
	std::string name;
	char no_use;
	int openConsole;

	getline(paraFile,strOneLine);
	extractString.clear();
	extractString.str(strOneLine);

	extractString>>name>>no_use>>openConsole;
	m_iOpenConsole = openConsole;
}

void InitializeParameter::WriteOtherParameter( std::fstream& paraFile )
{
	paraFile<<"[Other]"<<std::endl;

	paraFile<<"OpenConsole = "<<m_iOpenConsole<<std::endl;
}

int InitializeParameter::OpenConsole()
{
	return m_iOpenConsole;
}

void InitializeParameter::SetCEGUIPath( const std::string& name,const std::string pathName )
{
	if(name == "schemesPath")
	{
		m_strSchemesPath = pathName;
	}
	else if(name == "imagesetsPath")
	{
		m_strImagesetsPath = pathName;
	}
	else if(name == "fontsPath")
	{
		m_strFontsPath = pathName;
	}
	else if(name == "layoutsPath")
	{
		m_strLayoutsPath = pathName;
	}
	else if(name == "looknfeelsPath")
	{
		m_strLooknfeelPath = pathName;
	}
	else if(name == "luaScriptsPath")
	{
		m_strLuaScriptsPath = pathName;
	}
	else if(name == "schemasPath")
	{
		m_strXmlSchemasPath = pathName;
	}
}

void InitializeParameter::WriteCEGUIParameter( std::fstream& paraFile )
{
	paraFile<<"[CEGUI]"<<std::endl;

	paraFile<<"schemesPath = "<<m_strSchemesPath<<std::endl;
	paraFile<<"imagesetsPath = "<<m_strImagesetsPath<<std::endl;
	paraFile<<"fontsPath = "<<m_strFontsPath<<std::endl;
	paraFile<<"layoutsPath = "<<m_strLayoutsPath<<std::endl;
	paraFile<<"looknfeelsPath = "<<m_strLooknfeelPath<<std::endl;
	paraFile<<"luaScriptsPath = "<<m_strLuaScriptsPath<<std::endl;
	paraFile<<"schemasPath = "<<m_strXmlSchemasPath<<std::endl;
}

void InitializeParameter::ReadCEGUIParameter( std::fstream& paraFile )
{
	std::istringstream	extractString;
	std::string strOneLine;
	std::string name,pathName;
	char no_use;

	for (int i=0;i<7;++i)
	{
		getline(paraFile,strOneLine);
		extractString.clear();
		extractString.str(strOneLine);

		extractString>>name>>no_use>>pathName;
		SetCEGUIPath(name,pathName);
	}
}

std::string InitializeParameter::GetCEGUISchemesPath()
{
	return m_strSchemesPath;
}

std::string InitializeParameter::GetCEGUIImagesetsPath()
{
	return m_strImagesetsPath;
}

std::string InitializeParameter::GetCEGUIFontsPath()
{
	return m_strFontsPath;
}

std::string InitializeParameter::GetCEGUILayoutsPath()
{
	return m_strLayoutsPath;
}

std::string InitializeParameter::GetCEGUILooknfeelPath()
{
	return m_strLooknfeelPath;
}

std::string InitializeParameter::GetCEGUILuaScriptsPath()
{
	return m_strLuaScriptsPath;
}

std::string InitializeParameter::GetCEGUIXmlSchemasPath()
{
	return m_strXmlSchemasPath;
}
